package cjq.demo.thread;

/**
 * 代码中，主线程和读线程都访问共享变量
 * ReadThread可能会持续循环？
 */
public class NoVisibility {
    private static boolean ready;
    private static int number;
    private static class ReadThread extends Thread{
        public void run(){
            while(!ready){
                Thread.yield();
            }
            System.out.println(number);
        }
    }

    public static void setReady(boolean _ready){
        ready = _ready;
    }

    public static void setNumber(int _number){
        number = _number;
    }

    public static void main(String args[]){
        new ReadThread().start();
        number = 2;
        ready = true;
    }
}
